package texas_holdem.data;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import texas_holdem.data.Card.CardNumber;
import texas_holdem.data.Card.CardType;

import texas_holdem.game.Hand;



public class Odds {

	public enum PokerStep { Start, Flop, Turn, River};
	
	private HashMap<Hand, Double> _oddsBeforeFlop;
	private HashMap<Hand, Double> _oddsAfterFlop;
	private HashMap<Hand, Double> _oddsAfterTurn;
	private HashMap<Hand, Double> _oddsAfterRiver;
	
	
	public Odds(String start, String flop, String turn, String river)
	{
		_oddsBeforeFlop = readOddsFromFile(start);
		_oddsAfterFlop  = readFromFile(flop);
		_oddsAfterTurn = readFromFile(turn);
		_oddsAfterRiver = readFromFile(river);
	}
	
	private HashMap<Hand, Double> readFromFile(String flop) {
		// TODO Auto-generated method stub
		return null;
	}

	public double getOdds(Hand h, PokerStep ps)
	{
		switch (ps)
		{
			case Start:
				return _oddsBeforeFlop.get(h).doubleValue();
			case Flop:
				return _oddsAfterFlop.get(h).doubleValue();
			case Turn:
				return _oddsAfterTurn.get(h).doubleValue();
			case River:
				return _oddsAfterRiver.get(h).doubleValue();
		}
		// Error state - shouldn't get here
		return -1;	
	}
	
	private HashMap<Hand, Double> readOddsFromFile(String filename) {
		HashMap<Hand, Double> oddsMap = new HashMap<Hand,Double>();
		if (filename.equals("")) return null;
		try{
			  // Open the file that is the first 
			  // command line parameter
			  FileInputStream fstream = new FileInputStream(filename);
			  // Get the object of DataInputStream
			  DataInputStream in = new DataInputStream(fstream);
			  BufferedReader br = new BufferedReader(new InputStreamReader(in));
			  String strLine;
			  CardType[] types = CardType.values();
			  //Read File Line By Line
			  while ((strLine = br.readLine()) != null)   {
			  // Print the content on the console
				  String [] line = strLine.split(" ");
				  if (line[1].equals(line[2]))
				  {
					  // handle pair
					  CardNumber number = getCardNumber(line[1]);
					  Double odds = Double.valueOf(line[0]);
					  //System.out.println(odds);
					  for (int i = 0; i < types.length; i++)
					  {
						  Card c1 = new Card(number,types[i]);
						  for (int j = i+1; j < types.length; j++)
						  {
							  Card c2 = new Card(number,types[j]);
							  Hand h = new Hand(c1, c2, odds);
							  oddsMap.put(h, odds);
						  }
					  }
					  
				  }
				  else if (line[3].equals("unsuited"))
				  {
					  CardNumber n1 = getCardNumber(line[1]);
					  CardNumber n2 = getCardNumber(line[2]);
					  Double odds = Double.valueOf(line[0]);
					  //System.out.println(odds);
					  for (int i = 0; i < types.length; i++)
					  {
						  Card c1 = new Card(n1,types[i]);
						  for (int j = 0; j < types.length; j++)
						  {
							  if (i != j)
							  {
								  Card c2 = new Card(n2,types[j]);
								  Hand h = new Hand(c1, c2, odds);
								  oddsMap.put(h, odds);
							  }
							  
						  }
					  }
				  }
				  else
				  {
					  CardNumber n1 = getCardNumber(line[1]);
					  CardNumber n2 = getCardNumber(line[2]);
					  Double odds = Double.valueOf(line[0]);
					  //System.out.println(odds);
					  for (int i = 0; i < types.length; i++)
					  {
						    Card c1 = new Card(n1,types[i]);
							Card c2 = new Card(n2,types[i]);
							Hand h = new Hand(c1, c2, odds);
							oddsMap.put(h, odds);
					  }
				  }
			  }
			  //Close the input stream
			  in.close();
			    }catch (Exception e){//Catch exception if any
			  System.err.println("Error: " + e.getMessage());
			  }
		
		return oddsMap;
	}
	public static CardNumber getCardNumber(String card) {
		
		try
		{
			return CardNumber.values()[Integer.valueOf(card)-1];
		}
		catch (NumberFormatException e)
		{
			if (card.equals("A"))
				return CardNumber.values()[0];
			if (card.equals("T"))
				return CardNumber.values()[9];
			if (card.equals("J"))
				return CardNumber.values()[10];
			if (card.equals("Q"))
				return CardNumber.values()[11];
			if (card.equals("K"))
				return CardNumber.values()[12];
		}
		return null;
	}

	
}
